package com.wenlin.myproduction.binder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.bean.NumberBean;
import com.wenlin.myproduction.util.UI;

import me.drakeet.multitype.ItemViewBinder;

public class NumberResultBinder extends ItemViewBinder<NumberBean, NumberResultBinder.VH> {
    private OnClickListener listener;

    public NumberResultBinder(OnClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected VH onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new VH(inflater.inflate(R.layout.item_number, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull VH holder, @NonNull NumberBean item) {
        UI.setText(holder.tv, item.getNumber() >= 0 ? String.valueOf(item.getNumber()) : "");
        if (item.getNumber() != 9) {
            UI.setTextColor(holder.tv, item.isSelected() ? R.color.myGreen : R.color.myWhite);
        } else {
            UI.setTextColor(holder.tv, R.color.myGray);
        }
        UI.setOnClickListener(holder.tv, v -> {
            if (listener != null) listener.onClick(item);
        });
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView tv;

        public VH(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }

    public interface OnClickListener {
        void onClick(NumberBean bean);
    }
}
